package com.example.service.impl;

import com.example.entity.dto.UserDTO;
import com.example.entity.retail.CommentRetail;
import com.example.entity.retail.PostRetail;
import com.example.entity.retail.UserRetail;
import com.example.entity.retail.dto.CommentPostRetailDTO;
import com.example.entity.retail.dto.DescPostRetailDTO;
import com.example.entity.retail.dto.ItemPostRetailDTO;
import com.example.entity.retail.dto.MainPostRetailDTO;
import com.example.mapper.UserMapper;
import com.example.mapper.dao.RetailDao;
import com.example.service.RetailService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@Service
public class RetailServiceImpl implements RetailService {

    @Resource
    private RetailDao retailDao;

    @Resource
    private UserMapper userMapper;

    @Override
    public void add(PostRetail postRetail)
    {
        String retail_uid = UUID.randomUUID().toString().replace("-", "");
        postRetail.setRetailUid(retail_uid);
        System.out.println(postRetail);
        retailDao.add(postRetail);
    }

    @Override
    public List<MainPostRetailDTO> selectPostAll(String uid)
    {
        //种农圈首页DTO
        List<MainPostRetailDTO> mainPostRetailDTOS=new ArrayList<>();

        List<PostRetail> list=new ArrayList<>();

        if(uid==null){
            System.out.println("所有帖子");
            list= retailDao.selectPostAll();
        }
        else{
            System.out.println("用户帖子");
            list=retailDao.selectPostByUserUid(uid);
        }


        for (PostRetail postRetail : list) {
            UserDTO userDTO = userMapper.GetUserInfo(postRetail.getUid());
            postRetail.setUid(userDTO.getName());

            //数据转换
            MainPostRetailDTO mainPostRetailDTO = new MainPostRetailDTO();

            mainPostRetailDTO.setRetailUid(postRetail.getRetailUid());
            mainPostRetailDTO.setRetailTitle(postRetail.getRetailTitle());
            mainPostRetailDTO.setRetailImage(postRetail.getRetailImage());
            mainPostRetailDTO.setRetailAuthorName(userDTO.getName());
            mainPostRetailDTO.setRetailAuthorAvatar(userDTO.getAvatar());

            Integer count = retailDao.selectPostLikeCount(postRetail.getRetailUid());

            mainPostRetailDTO.setRetailLikes(count);

            mainPostRetailDTOS.add(mainPostRetailDTO);
        }
        return mainPostRetailDTOS;
    }

    @Override
    public void addComment(CommentRetail commentRetail)
    {
        String commentUid = UUID.randomUUID().toString().replace("-", "");
        commentRetail.setCommentUid(commentUid);
        retailDao.addComment(commentRetail);
    }

    @Override
    public DescPostRetailDTO selectPostDescByUid(String retailUid)
    {
        System.out.println("我是帖子UID"+retailUid);
        DescPostRetailDTO descPostRetailDTO = retailDao.selectPostDescByUid(retailUid);

        UserDTO userDTO=userMapper.GetUserInfo(descPostRetailDTO.getUid());
        descPostRetailDTO.setRetailAuthorAvatar(userDTO.getAvatar());
        descPostRetailDTO.setRetailAuthorName(userDTO.getName());

        return descPostRetailDTO;
    }

    @Override
    public List<CommentPostRetailDTO> selectPostCommentByUid(String postItem)
    {
        System.out.println("筛选获取主评论");
        List<CommentPostRetailDTO> commentPostRetailDTOS=retailDao.selectPostCommentByUid(postItem);

        System.out.println("筛选获取子评论");
        for (CommentPostRetailDTO commentPostRetailDTO : commentPostRetailDTOS)
        {
            //回复用户UID替换昵称
            UserDTO userDTO=userMapper.GetUserInfo(commentPostRetailDTO.getCommentUser());
            commentPostRetailDTO.setCommentUser(userDTO.getName());
            commentPostRetailDTO.setCommentUserAvatar(userDTO.getAvatar());

            Set<CommentRetail> commentRetails=retailDao.selectCommentReplyByUid(commentPostRetailDTO.getCommentUid());
            for (CommentRetail commentRetail : commentRetails)
            {
                UserDTO userDTO1=userMapper.GetUserInfo(commentRetail.getCommentUser());
                commentRetail.setCommentUser(userDTO1.getName());
                commentRetail.setCommentUserAvatar(userDTO1.getAvatar());
            }
            commentPostRetailDTO.setReplyTos(commentRetails);
        }

        return commentPostRetailDTOS;
    }

    @Override
    public ItemPostRetailDTO selectPostItemByUid(String retailUid)
    {
        PostRetail postRetail=retailDao.selectPostByUid(retailUid);
        ItemPostRetailDTO itemPostRetailDTO=retailDao.selectItemById(postRetail.getRetailCategory());
        if(itemPostRetailDTO==null)
        {
            return null;
        }
        else{
            itemPostRetailDTO.setRetailUid(retailUid);
            return itemPostRetailDTO;
        }
    }
}
